package com.cwnu.service.impl;

import com.cwnu.entity.Article;
import com.cwnu.entity.User;
import com.cwnu.mapper.ArticleMapper;
import com.cwnu.entity.PageBean;
import com.cwnu.service.ArticleService;
import com.cwnu.utils.ThreadLocalUtil;
import com.cwnu.utils.TokenUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.print.attribute.standard.JobKOctets;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public void add(Article article) {
        // 补充属性值
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        Integer userId = TokenUtil.getUserId();
        article.setCreateId(userId);
        articleMapper.add(article);
    }

    @Override
    public Article findById(Integer id) {
        return articleMapper.findById(id);
    }

    @Override
    public void update(Article article) {
        article.setUpdateTime(LocalDateTime.now());
        articleMapper.update(article);
    }

    @Override
    public void delete(Integer id) {
        articleMapper.delete(id);
    }

    @Override
    public PageBean<Article> list(Integer pageNum, Integer pageSize, Integer categoryId) {
        // 1、创建pageBean对象
        PageBean<Article> pb = new PageBean<>();

        // 2、开启分页查询，使用pageHelper插件
        PageHelper.startPage(pageNum, pageSize);

        // 3、调用mapper
        Integer userId = TokenUtil.getUserId();
        List<Article> as = articleMapper.list(userId, categoryId);
        // page中提供了方法，可以获取总记录条数和当前页数据
        Page<Article> p = (Page<Article>) as;
        // 把数据填充到pageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public PageBean<Article> listAll(Integer pageNum, Integer pageSize, Integer categoryId, Integer status) {
        // 1、创建pageBean对象
        PageBean<Article> pb = new PageBean<>();
        // 2、开启分页查询，使用pageHelper插件
        PageHelper.startPage(pageNum, pageSize);
        // 3、调用mapper
        List<Article> as = articleMapper.listAll(categoryId, status);
        // page中提供了方法，可以获取总记录条数和当前页数据
        Page<Article> p = (Page<Article>) as;
        // 把数据填充到pageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public List<Article> getWithoutPb() {
        Map<String, Object> mp= ThreadLocalUtil.get();
        Integer userId = (Integer) mp.get("id");
        return articleMapper.listWithoutPb(userId);
    }

    @Override
    public User geUser(Integer articleId) {
        return articleMapper.getUserByArticleId(articleId);
    }

    @Override
    public List<Article> getReviewArticle(Integer userId) {
        return articleMapper.getReviewArticle(userId);
    }

    @Override
    public List<Article> search(String key) {
        return articleMapper.search(key);
    }
}
